import pdfkit


# path_wkthmltopdf = r'D:\\Program Files\\wkhtmltopdf\\bin\\wkhtmltopdf.exe'
path_wkthmltopdf = r'D:\\Program Files\\wkhtmltopdf_0_12_2_1\\bin\\wkhtmltopdf.exe'


class DataBox:
    def __init__(self):
        self.nameList = []
        self.valueList = []

class HtmlGenerator:
    def __init__(self):
        self.head = ''
        self.body = ''

        self.projectMessage = DataBox()
        self.checkResult = DataBox()
        self.visableData = DataBox()

        pass

    #增加项目信息，按顺序
    def addProjectMessage(self,name,value):
        self.projectMessage.nameList.append(name)
        self.projectMessage.valueList.append(value)

    #增加检测结果
    def addCheckResult(self,name,value):
        self.checkResult.nameList.append(name)
        self.checkResult.valueList.append(value)

    #增加可视化数据
    def addVisabelData(self,name,value):
        self.visableData.nameList.append(name)
        self.visableData.valueList.append(value)


    def constructHtml(self,fileName):
        self.head = self.readHead('head.html')
        self.body = self.constructBody()

        str = '\
               <!doctype html>\
                <html lang=\"zh-CN\">\
                '
        str += self.head
        str += self.body
        str += '</html>'

        with open(fileName,mode='w',encoding='utf-8') as f:
            f.write(str)


    #读取head部分html
    def readHead(self,headFile):
        content = ''
        with open(headFile,mode='r',encoding='utf-8') as f:
            content = f.read()
        return content

    # 构建body
    def constructBody(self):
        ret = '<body>'
        # 大标题
        ret += self.constructTitle(id='title',value='检测报告单')

        # 第一个小标题:项目信息
        ret += self.constructSubTitle(id="subTitle0",value='项目信息')
        # 具体内容
        ret += self.constructSection(id = 'projectMessage',line = 4,maxColumn = 2,content=self.projectMessage)

        # 第二个小标题:检测结果
        ret += self.constructSubTitle(id="subTitle1", value='检测结果')
        # 具体内容
        ret += self.constructSection(id = 'checkResult',line = 5,maxColumn = 3,content=self.checkResult)

        # 第三个小标题:指标可视化
        ret += self.constructSubTitle(id="subTitle2", value='指标可视化')
        # 具体内容
        ret += self.constructJS()

        # 结束
        ret += '</body>'

        return ret

    def constructSection(self,id,line,maxColumn,content:DataBox):
        ret = ''
        num = len(content.nameList)

        for i in range(0,min(maxColumn,num/line + 1)):
            ret += self.constructData(id=id + 'Name' + str(i), dataList=content.nameList[i * line:(i + 1)*line], isName=True)
            ret += self.constructData(id=id + 'Value' + str(i), dataList=content.valueList[i * line:(i + 1)*line], isName=False)

        return ret
    # 构建大标题
    def constructTitle(self,id,value):
        ret = f'\
        <div id="{id}">\
            <h1>\
                {value}\
            </h1>\
        </div>'
        return ret

    # 构建小标题
    def constructSubTitle(self,id,value):
        ret = f'\
            <div id="{id}" >\
                <hr/>\
                    {value}\
                <hr/>\
            </div>'
        return ret

    # 构建数据
    def constructData(self,id,dataList,isName):
        dataStr = ''
        for data in dataList:
            dataStr += data
            if isName:
                dataStr += ':'
            dataStr += '<br>'

        ret = f'\
            <div id="{id}" >\
                    {dataStr}\
            </div>'
        return ret

    # 构建js
    def constructJS(self):
        dataStr = ''
        for i in range(0,len(self.visableData.nameList)):
            dataStr += f"{{value:{self.visableData.valueList[i]}, name:\"{self.visableData.nameList[i]}\"}},"

        ret = "\
        <div id=\"main\" style=\"width: 500px;height:500px;\"></div>\
        <script type=\"text/javascript\">\
            var myChart = echarts.init(document.getElementById('main'));\
            var option = {\
                animation:false,\
                series : [\
                    {\
                        name: \'访问来源\',\
                        type: \'pie\',\
                        radius: \'55%\',\
                        data:["\
                            + dataStr + \
                            "],\
                        roseType: \'angle\',\
                        itemStyle: {\
                            normal: {\
                                shadowBlur: 200,\
                                shadowColor: \'rgba(0, 0, 0, 0.5)\'\
                            }\
                        }\
                    }\
                ]\
            };\
            myChart.setOption(option);\
        </script>"
        return ret

    '''将html文件生成pdf文件'''

    def html_to_pdf(self,html, to_file):
        # 将wkhtmltopdf.exe程序绝对路径传入config对象

        config = pdfkit.configuration(wkhtmltopdf=path_wkthmltopdf)
        # 生成pdf文件，to_file为文件路径
        options = {
                    "enable-local-file-access": None,
                    'footer-center': '[page] / [topage]',
                    'page-size': 'A4',
                    'header-spacing': '3',  # 设置页眉与正文之间的距离，单位是毫米
                    'footer-spacing': '3',  # 设置页眉与正文之间的距离，单位是毫米

            # 'header-right': 'Quality Report',  # 设置页眉右侧数据
                    'header-font-size': 10,  # 设置页眉字体大小
                    'footer-font-size': 10,  # 设置页脚字体大小
                    'margin-top': '0.75in',
                    'margin-right': '0.75in',
                    'margin-bottom': '0.75in',
                    'margin-left': '0.75in',
                    'encoding': "UTF-8",

                    # 'header-line': None,  # 为None时表示确定，生成页眉下的线
                   }
        pdfkit.from_file(html, to_file, configuration=config,options=options)
        print('完成')


if __name__ == "__main__":
    html_path = 'generate.html'

    html = HtmlGenerator()
    #
    # html.addProjectMessage(name='项目名称',value='中国测试项目')
    # html.addProjectMessage(name='版本', value='2.1')
    # html.addProjectMessage(name='建立时间', value='2021-10-01')
    # html.addProjectMessage(name='测评人', value='张三')
    # html.addProjectMessage(name='项目名称',value='中国测试项目')
    # html.addProjectMessage(name='版本', value='2.1')
    # html.addProjectMessage(name='建立时间', value='2021-10-01')
    # html.addProjectMessage(name='测评人', value='张三')
    # html.addProjectMessage(name='项目名称',value='中国测试项目')
    # html.addProjectMessage(name='版本', value='2.1')
    # html.addProjectMessage(name='建立时间', value='2021-10-01')
    # html.addProjectMessage(name='测评人', value='张三')
    #
    # html.addCheckResult(name='A能力',value='6.6')
    # html.addCheckResult(name='B能力', value='2.1')
    # html.addCheckResult(name='C能力', value='3.2')
    # html.addCheckResult(name='D能力', value='6.5')
    # html.addCheckResult(name='A能力',value='6.6')
    # html.addCheckResult(name='B能力', value='2.1')
    # html.addCheckResult(name='C能力', value='3.2')
    # html.addCheckResult(name='D能力', value='6.5')
    # html.addCheckResult(name='A能力',value='6.6')
    # html.addCheckResult(name='B能力', value='2.1')
    # html.addCheckResult(name='C能力', value='3.2')
    # html.addCheckResult(name='D能力', value='6.5')
    # html.addCheckResult(name='A能力',value='6.6')
    # html.addCheckResult(name='B能力', value='2.1')
    # html.addCheckResult(name='C能力', value='3.2')
    # html.addCheckResult(name='D能力', value='6.5')
    # html.addCheckResult(name='A能力',value='6.6')
    # html.addCheckResult(name='B能力', value='2.1')
    # html.addCheckResult(name='C能力', value='3.2')
    # html.addCheckResult(name='D能力', value='6.5')
    #
    #
    # html.addVisabelData(name='A指标',value='9.6')
    # html.addVisabelData(name='B指标', value='2.6')
    # html.addVisabelData(name='C指标', value='3.8')
    # html.addVisabelData(name='D指标', value='1.5')
    #
    # html.constructHtml(fileName=html_path)
    html.html_to_pdf(html_path, 'name.pdf')

    pass